package com.zh.boot05web01.controller;

import com.zh.boot05web01.bean.Person;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhanghuan
 * @date: 2022/9/19 20:20
 * @description: create it
 */
@RestController
public class ParameterTestController {


    /**
     * 数据绑定，页面提交的数据为什么可以绑定到对象里面？
     *
     * @param person
     * @return
     */

    @PostMapping("/saveuser")
    public Person saveUser(Person person) {
        return person;
    }

    @GetMapping("/car/{id}/owner/{username}")
    public Map<String, Object> getCar(@PathVariable("id") Integer id,
                                      @PathVariable("username") String username,
                                      @PathVariable Map<String, String> pv,
                                      @RequestHeader("User-Agent") String userAgent,
                                      @RequestHeader Map<String, String> headers,
                                      @RequestParam("age") Integer age,
                                      @RequestParam("inters") List<String> inters,
                                      @RequestParam Map<String, String> params,
                                      @CookieValue("password") String password,
                                      @CookieValue("password") Cookie cookie) {
        Map<String, Object> map = new HashMap<>();
        // map.put("id", id);
        // map.put("username", username);
        // map.put("pv", pv);
        // map.put("userAgent", userAgent);
        // map.put("headers", headers);
        map.put("age", age);
        map.put("inters", inters);
        map.put("params", params);
        map.put("password", password);
        System.out.println(cookie);
        return map;
    }

    @PostMapping("/save")
    public Map postMethod(@RequestBody String content) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        return map;
    }

    //1.语法：/cars/sell;low=34;brand=byd,audi,yd
    // 2.Springboot 默认是禁用矩阵变量的
    // 手动开启 原理：对于路径处理：UrlPathHelper进行解析
    // removeSemicolonContent 支持矩阵变量
    @GetMapping("cars/{path}")
    public Map carsSell(@MatrixVariable("low") Integer low, @MatrixVariable("brand") List<String> brand, @PathVariable("path") String path) {

        Map<String, Object> map = new HashMap<>();
        map.put("low", low);
        map.put("brand", brand);
        map.put("path", path);

        return map;
    }

    //   /boss/1;age=20/2;age=10
    @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age", pathVar = "bossId") Integer bossAge, @MatrixVariable(value = "age", pathVar = "empId") Integer empAge) {
        Map<String, Object> map = new HashMap<>();
        map.put("bossAge", bossAge);
        map.put("empAge", empAge);

        return map;
    }
}
